#!/usr/bin/env python
# encoding: utf-8

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from common.libs.Helper import getFormatTime

class MessageTableView(QTableView):
    def __init__(self, parent=None):
        '''表格初始化'''
        super(QTableView, self).__init__(parent)
        self.model = QStandardItemModel(0, 0)
        self.HeaderList = []
        self.setModel(self.model)
        # 下面代码让表格100填满窗口
        self.horizontalHeader().setStretchLastSection(True)
        self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.isClearChooseLine_Flag = False
        self.setHeader(['事件', '主题', '消息', '请求服务质量', '时间'])

    def setHeader(self, header=[]):
        self.model.setHorizontalHeaderLabels(header)  #

    def clear_data(self):
        self.model.clear()
        self.model = QStandardItemModel(0, 0)
        self.setModel(self.model)
        self.setHeader(['事件', '主题', '消息', '请求服务质量', '时间'])

    def showMessage(self, topic, payload, qos):
        rows = self.model.rowCount()  # 总行数
        self.model.setItem(rows, 1, QStandardItem(topic))
        self.model.setItem(rows, 2, QStandardItem(payload))
        self.model.setItem(rows, 3, QStandardItem(qos))
        self.model.setItem(rows, 4, QStandardItem(getFormatTime()))

